import { Reducer } from "redux";
import actionTypes from "../../store/actions/actionTypes";
import dayjs from "dayjs";
import { SettingsI, WeixinOauthI, UserI } from "../../types/system";
import {
  setSettingsAction,
  setUserAction,
  setWeixinOauthAction,
} from "../actions";

const initialSettings: SettingsI = {
  collapsedMenu: true,
  allowUpload: false,
  network: "LAN",
  breadcrumbs: [{ label: "首页", key: "/" }],
};

export const settingsReducer: Reducer<
  SettingsI,
  ReturnType<typeof setSettingsAction>
> = (state = initialSettings, action: ReturnType<typeof setSettingsAction>) => {
  switch (action.type) {
    case actionTypes.SET_SETTINGS: {
      return Object.assign({}, state, action.payload);
    }
    default:
      return state;
  }
};

const initialWeixinOauth: WeixinOauthI = {
  id: undefined,
  oauthType: "",
  openid: "",
  nickname: "",
  sex: 0,
  province: "",
  city: "",
  country: "",
  headimgurl: "",
  privileges: "",
  unionid: "",
  deleteFlag: false,
  sign: "",
  userID: "",
};

export const weixinOauthReducer = (
  state = initialWeixinOauth,
  action: ReturnType<typeof setWeixinOauthAction>
) => {
  switch (action.type) {
    case actionTypes.SET_WEIXIN_OAUTH: {
      return Object.assign({}, state, action.payload);
    }
    default:
      return state;
  }
};

export const initialUser: UserI = {
  id: undefined,
  accessToken: "",
  refreshToken: "",
  username: "",
  cellphone: "",
  email: "",
  nickname: "",
  lastName: "",
  firstName: "",
  fullName: "",
  birthday: new Date(),
  password: "",
  sex: 0,
  score: 0,
  avatarURL: "",
  lastLoginAt: new Date(),
  isActive: false,
  isSuperuser: false,
  currentRole: undefined,
  roleUsers: [],
  // tenantID: "",
  // tenant: null,
  depts: "[]",
  resources: [],
  // policies: [],
};

export const userReducer = (
  state = initialUser,
  action: ReturnType<typeof setUserAction>
) => {
  switch (action.type) {
    case actionTypes.SET_USER: {
      return Object.assign({}, state, action.payload);
    }
    default:
      return state;
  }
};
